package com.mao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mao.common.BaseContext;
import com.mao.entity.ShoppingCart;
import com.mao.mapper.ShoppingCartMapper;
import com.mao.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public void clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        this.remove(queryWrapper);
    }

    @Override
    public String add(ShoppingCart shoppingCart) {
        //log.info(shoppingCart.toString());
        //1.获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //1.2 设置用户Id
        shoppingCart.setUserId(userId);
        //2.判断添加的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        //2.1 先统一查询条件加用户ID
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        //2.2 不为空则添加的是菜品
        if (shoppingCart.getDishId() != null) {
            //2.3 查询一下是否以及添加了
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            queryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
            //2.4 菜品ID为空则是添加套餐
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        boolean isFT=false;
        ShoppingCart shoppingCart1 = this.getOne(queryWrapper);
        //3.判断购物车是否已经添加了该菜品或者套餐
        if (shoppingCart1!=null){
            //3.1 不为空，则已添加，直接将数量属性加1
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            isFT = this.update(shoppingCart1, queryWrapper);
        }else {
            //3.2 为空，则是没有添加过的，所以给他设置初始数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            isFT = this.save(shoppingCart);
        }
        if (!isFT){
            return "添加失败";
        }
        return "添加成功";
    }

    @Override
    public List<ShoppingCart> get_list() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public String sub(ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            queryWrapper.eq(shoppingCart.getDishFlavor()!=null,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        boolean isTF =false;
        ShoppingCart shoppingCart1 = this.getOne(queryWrapper);
        if (shoppingCart1.getNumber()==1){
            isTF = this.remove(queryWrapper);
        }else if (shoppingCart1.getNumber()>1){
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            isTF = this.update(shoppingCart1, queryWrapper);
        }
        if (!isTF){
            return "删除失败";
        }
        return "删除成功";
    }
}
